
/* The Projectile class provides basic functionality for projectiles spawned in abilities and will receive most of its values
 * from a Use() function from an ability
 * 
 * The projectile requires to have a collider set as a trigger and a rigidbody for the OnTriggerEnter function to call
 *
 * It causes an object to move forward untill a collision with an object is detected and deals damage if the target should be damaged
 * The projectile also destroys itself after a collision or after its maximum range is reached
 * On destruction the projectile will spawn an effect, which will usually be a particle emitter
 *
 * Author : Reinier
 */
 
private var currentDistance : float;			/* The current distance of the projectile to it's spawning location */
private var damage : int = 0;					/* The damage this projectile will deal */
private var deathEffect : GameObject;			/* The effect that will be spawned on death */
private var hitsMobs : boolean = false;			/* If the projectile will deal damage to mobs, if set to false, it ignores collisions with mobs entirely */
private var hitsPlayers : boolean = false;		/* If the projectile will deal damage to players, if set to false, it ignores collisions with players entirely */
private var maxDistance : float;				/* The maximum distance the projectile can travel from its spawning location */
private var origin : GameObject;				/* The GameObject that spawned this projectile */
private var originalLocation : Vector3;			/* The spawning location of this projectile */	
private var speed : int = 10;					/* The speed at which this projectile will move */

function Awake() {
	/* On awaking, the spawning location is saved */
	originalLocation = gameObject.transform.localPosition;
}

/* Every frame the distance to the spawning point is calculated,
 * if the distance exceeds the range, the projectile will autodestruct itself.
 * if the range is not yet exceeded it will move forward
 */
function Update () {
	/* Every frame the distance to the spawning point is calculated */
	currentDistance = Vector3.Distance(originalLocation, gameObject.transform.localPosition);
	
	/* If the projectile exceeds its range it should autodestruct */
	if(currentDistance > maxDistance){
		Die();
	}
	else{
		/* If the range is not yet reached, the projectile will move forward equal to its speed per second (NOT speed per frame!)*/
		transform.Translate(Vector3.forward * Time.deltaTime * speed);
	}
}

/* When another collider enters the trigger area of the projectile, OnTriggerEnter is called
 * If the projectile collides with an object it should damage, it destroys itself and damages the target
 * Else the projectile simply destroys itself
 *
 * The projectile will ignore collisions with players and enemies if these objects should not be hit, it also ignores collision with other projectiles */
function OnTriggerEnter(other : Collider) {
	if("Enemy" == other.gameObject.tag) {
		if(hitsMobs) {
			/* If the projectile should hit enemies, it will deal damage and destroy itself */
			doDamage(other);
			Die();
			return;
		}
		else{
			/* If the projectile should not hit enemies, the collision is ignored and the projectile will pass through enemies */
			return;
		}
	}
	
	if("Player" == other.gameObject.tag) {
		if(hitsPlayers) {
			/* If the projectile should hit players, it will deal damage and destroy itself */
			doDamage(other);
			Die();
			return;
		}
		else{
			/* If the projectile should not hit enemies, the collision is ignored and the projectile will pass through players */
			return;
		}
	}
	
	/* The projectile should ignore collisions with other projectiles */
	if("Projectile" == other.gameObject.tag) {
		return;
	}
	
	/* If the projectile collides with something that is NOT an enemy, player or projectile, the projectile should destroy itself */
	Die();
}

/* The doDamage function simply checks if the collider has stats, and if so, deals damage to the gameobject */
function doDamage(other : Collider) {
	var targetStats : CharacterStats = other.gameObject.GetComponent(CharacterStats);
		if(targetStats) {
			targetStats.OnDamage(damage, origin);
		}
}

/* The Die function destroys the projectile and displays a death effect if one is set */
function Die(){
	if(deathEffect) {
		Instantiate(deathEffect, transform.localPosition, transform.localRotation);
	}
	
	Destroy(gameObject);
}


/* Basic setter for the private variable */
function SetHitEffect(newEffect : GameObject) {
	deathEffect = newEffect;
}

/* Basic setter for the private variable */
function HitsMobs(newBool : boolean) {
	hitsMobs = newBool;
}

/* Basic setter for the private variable */
function HitsPlayers(newBool : boolean) {
	hitsPlayers = newBool;
}

/* Basic setter for the private variable */
function SetSpeed(newSpeed : int) {
	speed = newSpeed;
}

/* Basic setter for the private variable */
function SetDamage(newDamage : int) {
	damage = newDamage;
}

/* Basic setter for the private variable */
function SetOrigin(newOrigin : GameObject) {
	origin = newOrigin;
} 

/* Basic setter for the private variable */
function SetMaxDistance(newDistance : float) {
	maxDistance = newDistance;
}